﻿#include "CameraCommon/VideoFrame.h"
#include <QMutex>

namespace Qly
{
VideoFrame::VideoFrame(uint8_t *imgBuf, QSize size, AVPixelFormat f)
{
    pData[0] = imgBuf;
    width = size.width();
    height = size.height();
    format = f;
    switch (format) {
    case AV_PIX_FMT_PAL8:
    case AV_PIX_FMT_GRAY8:
        bytePerLine[0] = width;
        break;
    case AV_PIX_FMT_GRAY16:
        bytePerLine[0] = width * 2;
        break;
    case AV_PIX_FMT_BGR0:
    case AV_PIX_FMT_ABGR:
    case AV_PIX_FMT_0BGR:
    case AV_PIX_FMT_BGRA:
    case AV_PIX_FMT_0RGB:
    case AV_PIX_FMT_RGBA:
    case AV_PIX_FMT_ARGB:
    case AV_PIX_FMT_RGB0:
        bytePerLine[0] = width * 4;
        break;
    case AV_PIX_FMT_RGB24:
    case AV_PIX_FMT_BGR24:
        bytePerLine[0] = width * 3;
        break;
    case AV_PIX_FMT_BAYER_BGGR8:
    case AV_PIX_FMT_BAYER_GBRG8:
    case AV_PIX_FMT_BAYER_GRBG8:
    case AV_PIX_FMT_BAYER_RGGB8:
        bytePerLine[0] = width * 1;
        break;
    case AV_PIX_FMT_BAYER_BGGR16BE:
    case AV_PIX_FMT_BAYER_BGGR16LE:
    case AV_PIX_FMT_BAYER_GBRG16BE:
    case AV_PIX_FMT_BAYER_GBRG16LE:
    case AV_PIX_FMT_BAYER_GRBG16BE:
    case AV_PIX_FMT_BAYER_GRBG16LE:
    case AV_PIX_FMT_BAYER_RGGB16BE:
    case AV_PIX_FMT_BAYER_RGGB16LE:
        bytePerLine[0] = width * 2;
        break;
    case AV_PIX_FMT_YUYV422:
    case AV_PIX_FMT_UYVY422:
        bytePerLine[0] = width * 2;
        break;
    default:
        break;
    }
}
VideoFrame::VideoFrame(uint8_t *imgBuf, int stride, QSize size, AVPixelFormat f)
{
    pData[0] = imgBuf;
    bytePerLine[0] = stride;
    width = size.width();
    height = size.height();
    format = f;
}

void VideoFrame::setData(uint8_t *imgBuf, int stride, QSize size, AVPixelFormat f, QMutex *mutex)
{
    pData[0] = imgBuf;
    bytePerLine[0] = stride;
    width = size.width();
    height = size.height();
    format = f;
    pMutex = mutex;
}

VideoFrame::VideoFrame(uint8_t *imgBuf, int stride, QSize size, AVPixelFormat f, QMutex *mutex)
    :VideoFrame(imgBuf, stride, size, f)
{
    pMutex = mutex;
}

}
